<?php

/**
 * Description of reis
 *
 * @author alex
 */
class reis extends CI_Controller {

    //Opvulling voor headertemplate. TODO: verplaatsen naar eigen library, net als reisfilter?
    private $headerData;

    //Alle standaard zaken in constructor vast klaarzetten
    public function __construct() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('menu_helper');
        $this->headerData = array('title' => 'Avontuurlijke groepsreizen, rondreizen en singlesreizen  | Shoestring Groepsreizen',
            'menu' => getMenu());
        $this->load->model('Reis_model', 'model');

        $this->load->library('reisfilter');
        //pagination
        $this->load->library('pagination');
    }

    //De standaard aangeroepen functie voor deze controller
    public function index($offset = 0) {
        $reisfilters = $this->input->post('reisfilter');

        if ($reisfilters) {
            //Lege stringwaarden strippen uit het array
            $reisfilters = array_filter($reisfilters, 'strlen');
        }
        //Aantal regels per pagina
        $config['base_url'] = base_url() . 'reis/page';
        $config['per_page'] = 3;

        $returnData = $this->model->getReizen($reisfilters, $offset, $config['per_page']);
        //Lees totaal aantal rijen in
        $config['total_rows'] = $returnData['num_rows'];
        $this->pagination->initialize($config);

        $reiscontent = $this->load->view('reis_view', array('reizen' => $returnData['result'], 'selected' => $reisfilters, 'pagination' => $this->pagination->create_links()), true);


        if ($this->input->post('ajax')) {
            echo $reiscontent;
        } else {
            $this->execute($reiscontent);
        }




//        $reisfilters = $this->input->post('reisfilter');
//
//        if ($reisfilters) {
//            //Lege stringwaarden strippen uit het array
//            $reisfilters = array_filter($reisfilters, 'strlen');
//        }
//
//        $reiscontent = $this->load->view('reis_view', array('reizen' => $this->model->getReizen($reisfilters), 'selected' => $reisfilters), true);
//
//        $this->execute($reiscontent);
    }

    public function page($offset = 0) {
        $reisfilters = $this->input->post('reisfilter');
//        var_dump($reisfilters);
        session_unset();
        
        if ($reisfilters) {
            //Lege stringwaarden strippen uit het array
            $reisfilters = array_filter($reisfilters, 'strlen');
        }
        //Aantal regels per pagina
        $config['base_url'] = base_url() . 'reis/page';
        $config['per_page'] = 3;

        $returnData = $this->model->getReizen($reisfilters, $offset, $config['per_page']);
        //Lees totaal aantal rijen in
        $config['total_rows'] = $returnData['num_rows'];
        $this->pagination->initialize($config);

        $reiscontent = $this->load->view('reis_view', array('reizen' => $returnData['result'], 'selected' => $reisfilters, 'pagination' => $this->pagination->create_links()), true);


        if ($this->input->post('ajax')) {
            echo $reiscontent;
        } else {
            $this->execute($reiscontent);
        }
    }

    public function detail($reisID) {
        if (!isset($reisID)) {
            redirect();
        }
        $planningcontent = $this->load->view('planning_view', array('planningen' => $this->model->getReisPlanningen($reisID)), true);
        $reiscontent = $this->load->view('detail_view', array('detail' => $this->model->getReisDetail($reisID),
            'planning' => $planningcontent), true);

        if (!$reiscontent) {
            //Geen reiscontent gevonden bij gegeven reisID, dus terug naar home
            redirect();
        }

        $this->execute($reiscontent);
    }

    private function execute($content = '') {
        $this->load->view('header', $this->headerData);

        $this->load->view('container_view', array(
            'leftcolumn' => $this->reisfilter->getFilterBlob(),
            'rightcolumn' => $content
        ));

        $this->load->view('footer');
    }

}

?>
